package cn.wolfcode.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.enums.AppointmentEnum;
import cn.wolfcode.enums.ConsumptionEnum;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private IConsumptionService consumptionService;


    @RequiredPermission(name="预约单页面",expression="appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo){
        //预约单所有状态
        AppointmentEnum[] values = AppointmentEnum.values();
        model.addAttribute("values",values);

        List<SystemDictionaryItem> businessBig = systemDictionaryItemService.selectBySn("business");
        model.addAttribute("businessBig",businessBig);

        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses",businesses);
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "appointment/list";
    }


    @RequiredPermission(name="预约单删除",expression="appointment:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }


    @RequiredPermission(name="预约单新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {
        if (appointment.getId() == null) {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//            String createTime = sdf.format(new Date());
//            String ano = createTime + (int)(Math.random()*100000);

            String createTime = DateUtil.format(new Date(), "yyyyMMddHHmmss");
            String ano = createTime + RandomUtil.randomNumbers(5);
            appointment.setAno(ano);
            appointment.setStatus(0);
            appointment.setCreateTime(new Date());

            System.out.println(appointment);
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }


    @RequiredPermission(name="预约单新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("appointment",appointmentService.get(id));
        }
        return "appointment/input";
    }


    @RequestMapping("/inOrOutAppointment")
    public String inOrOutAppointment(Integer status,Long id, String key){
        //待确认
        if(status==0){
            //如果是确认预约
            if(key.equals("confirm")){
                appointmentService.setStatus(AppointmentEnum.PERFORM.getValue(),id);
            }
            if(key.equals("cancel")){
                appointmentService.setStatus(AppointmentEnum.DISCARD.getValue(),id);
            }
        }
        return "redirect:/appointment/list";
    }

    @RequestMapping("/createConsumption")
    public String createConsumption(Model model, Long id){
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses",businesses);

        //从预约单中拿数据
        Appointment appointment = appointmentService.get(id);
        appointmentService.setStatus(AppointmentEnum.CONSUME.getValue(),id);

        //生成消费单
        Consumption consumption = new Consumption();
        //设置消费单状态为待结算
        consumption.setStatus(ConsumptionEnum.COUNT.getValue());
        //生成消费单流水号
        String createTime = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        String cno = createTime + RandomUtil.randomNumbers(5);
        consumption.setCno(cno);

        //客户名称
        consumption.setCustomerName(appointment.getContactName());
        //客户电话
        consumption.setCustomerTel(appointment.getContactTel());
        //消费门店
        consumption.setBusiness(appointment.getBusiness());
        //预约单流水号
        consumption.setAppointmentAno(appointment.getAno());
        //创建时间
        consumption.setCreateTime(new Date());
        //结算时间
        consumption.setPayTime(new Date());
        //审核时间
        consumption.setAuditTime(new Date());
        //结算人
        consumption.setPayee(UserContext.getCurrentUser());
        //审核人
        consumption.setAuditor(UserContext.getCurrentUser());
        //consumptionService.save(consumption);

        consumptionService.save(consumption);
        //System.out.println(consumption);
        model.addAttribute("consumption",consumption);

        //跳转到消费单input页面, 回显消费单相关数据
        return "/consumption/input";
    }
}
